package com.ruoyi.merchant.service.impl;

import com.ruoyi.common.core.cg.pojo.*;
import com.ruoyi.merchant.dto.SaveRequest;
import com.ruoyi.merchant.dto.UserDTO;
import com.ruoyi.merchant.mapper.*;
import com.ruoyi.merchant.service.UsersService;
import com.ruoyi.merchant.utils.MD5Utils;
import com.ruoyi.merchant.utils.Result;
import com.ruoyi.merchant.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* @author ？？？
* @description 针对表【users】的数据库操作Service实现
* @createDate 2025-03-04 14:04:13
*/
@Service
public class UsersServiceImpl
    implements UsersService {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private MD5Utils md5Utils;
    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private LeaderMapper leaderMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private TCommunityLeaderMapper communityLeaderMapper;
    @Autowired
    private TLeaderStoreMapper leaderStoreMapper;
    @Autowired
    private TCommunityStoreMapper communityStoreMapper;

    @Override
    public Result loginUser(String phone) {
        Random random = new Random();
        int i = random.nextInt(89999) + 10000;
        Leader loginUser = usersMapper.loginUser(phone);
        if(loginUser == null){
            Leader leader = new Leader();
            leader.setPhone(phone);
            leader.setInvitationCode(String.valueOf(i));
            int saveLeader = usersMapper.saveLeader(leader);
            Integer uid = leader.getId();
            Map<String,Object> map = new HashMap<>();
            map.put("uid",uid);
            System.err.println("添加获取的主键id"+uid);
            return Result.build(100,"创建成功",map);
        }
        Integer uid = loginUser.getId();
        Map<String,Object> map = new HashMap<>();
        map.put("uid",uid);
        System.err.println("数据库中的id"+uid);
        return Result.build(200,"登录成功",map);
    }


    @Override
    public Result login(String phone, String password) {
        Users users = usersMapper.login(phone);

        System.err.println("users = " + users);
        if(users == null){
            return Result.error("用户名不存在");
        }
        String usersPassword = users.getPassword();
        String pwd = md5Utils.digest(password);
        if(!usersPassword.equals(pwd)){
            return Result.error("密码错误");
        }

        String token = tokenUtils.generateToken(String.valueOf(users.getUid()));
        Map<String,Object> map = new HashMap<>();
        map.put("userId",users.getUid());
        map.put("username",users.getNickname());
        map.put("phone",users.getPhone());
        map.put("email",users.getEmail());
        map.put("token",token);
        return Result.build(200,"登录成功",map);
    }

    @Override
    public Result UserSave(Users users) {
        String password = users.getPassword();
        users.setPassword(md5Utils.digest(password));
        int userSave = usersMapper.UserSave(users);
        return userSave > 0 ? Result.ok() : Result.error("添加失败");
    }

    @Override
    public Result selectById(Integer uid) {
        UserDTO userDTO = usersMapper.selectById(uid);
        if(userDTO == null){
            return Result.error("用户不存在");
        }
        return Result.ok(userDTO);
    }

    @Override
    @Transactional
    public Result saveData(Community community, Leader leader, Store store) {
        leader.setCreateTime(new Date());
        leader.setMoney( new BigDecimal(0));
        int leaderSave = leaderMapper.leaderSave(leader);
        System.out.println("leaderSave = " + leaderSave);
        //获取leader的id
        Integer lid = leader.getId();
        System.err.println(lid);
        Community comByLid = communityMapper.selectComByLid(lid);
        //判断community中的lid为空的话
        if(comByLid == null){
            community.setLeaderId(lid);
        }
        int communitySave = communityMapper.communitySave(community);
        System.out.println( "communitySave = " + communitySave);
        //获取community的id
        Integer cid = community.getId();
        System.err.println(cid);

        store.setCreateTime(new Date());
        store.setUpdateTime(new Date());
        store.setPrice(new BigDecimal(0));
        int storeSave = storeMapper.storeSave(store);
        System.out.println( "storeSave = " + storeSave);
        //获取store的id
        Integer sid = store.getId();
        System.err.println(sid);

        TCommunityLeader tCommunityLeader = new TCommunityLeader();
        tCommunityLeader.setCommunityId(cid);
        tCommunityLeader.setLeaderId(lid);
        //添加中间表
        communityLeaderMapper.comLeaderSave(tCommunityLeader);

        TLeaderStore tLeaderStore = new TLeaderStore();
        tLeaderStore.setLeaderId(lid);
        tLeaderStore.setStoreId(sid);
        //添加中间表
        leaderStoreMapper.leaderStoreSave(tLeaderStore);

        //添加中间表
        TCommunityStore tCommunityStore = new TCommunityStore();
        tCommunityStore.setCommunityId(cid);
        tCommunityStore.setStoreId(sid);
        communityStoreMapper.comStoreSave(tCommunityStore);

        return Result.ok();
    }

//    @Override
//    @Transactional
//    public int communitySave(Community community) {
//        int save = communityMapper.communitySave(community);
//        return save;
//    }

//    @Override
//    public Result saveRequest(SaveRequest saveRequest) {
//
//    }


}




